package com.wx.activitys.base.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.alibaba.fastjson.JSONObject;
import com.wx.activitys.base.model.SSMResponse;


public class SMSUtil {
	
	public static boolean sendSMS(String mobile, String content) { 
		//短信接口1
        PostMethod postMethod = new PostMethod("http://sms.dzd.com/v4/sms/send.do");
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
        String timeStamp = format.format(new Date());
        String uid = "4119";
        String key = "e1b624f0cc91ca28bc157a8f304ba78a";
        postMethod.addParameter("uid", uid);
        postMethod.addParameter("timestamp",timeStamp);
        try {
            postMethod.addParameter("sign", BaseUtils.getSign(uid, key, timeStamp));//sign-为安全验证的密匙,需要计算
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        postMethod.addParameter("mobile", mobile);
        postMethod.addParameter("text", content);
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        try {
            httpClient.executeMethod(postMethod);
            System.out.println(postMethod.getResponseBodyAsString());
            String response = postMethod.getResponseBodyAsString();
            JSONObject jsStr = JSONObject.parseObject(response);
            SSMResponse ssmResponse = JSONObject.toJavaObject(jsStr,SSMResponse.class);
            if (ssmResponse.getCode().equals("0")){
                return true;
            }else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
	}  

}